//
//  YMLoginInputBoxView.m
//  YMMedical
//
//  Created by y s on 2023/7/13.
//

#import "YMLoginInputBoxView.h"


@interface YMLoginInputBoxView ()

/** 标题 */
@property (nonatomic, strong) UILabel *lblTitle;
/** 输入框 */
@property (nonatomic, strong) UITextField *tfMain;
/** 获取验证码 */
@property (nonatomic, strong) UIButton *btnCode;

@end

@implementation YMLoginInputBoxView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    self.backgroundColor = STheme.D9;
    self.layer.cornerRadius = [SFitUtil fit:8];
    self.clipsToBounds = YES;
    [self addSubview:self.lblTitle];
    [self addSubview:self.tfMain];
    [self addSubview:self.btnCode];
    
    [self.lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.centerY.mas_equalTo(0);
    }];
    [self.tfMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lblTitle.mas_right).offset([SFitUtil fit:25]);
        make.top.bottom.mas_equalTo(0);
        make.right.equalTo(self.btnCode.mas_left);
    }];
    [self.btnCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.bottom.mas_equalTo(self.tfMain);
        make.width.mas_equalTo(0);
    }];
    
    
}


#pragma mark -- public methods
/** 隐藏键盘 */
- (void)dismissKeyboard{
    [self.tfMain resignFirstResponder];
}
/** 弹出键盘 */
- (void)showKeyboard{
    [self.tfMain becomeFirstResponder];
}

- (void)setStrTitle:(NSString *)strTitle{
    _strTitle = strTitle;
    self.lblTitle.text = strTitle;
    CGFloat lblWidth = [SFitUtil sizeOf:self.lblTitle].width+[SFitUtil fit:5];
    [self.lblTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(lblWidth);
    }];
    
}
- (void)setStrPlace:(NSString *)strPlace{
    _strPlace = strPlace;
    if (strPlace != nil && strPlace.length > 0) {
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:strPlace attributes:@{NSFontAttributeName:[SFitUtil fitFont:16],NSForegroundColorAttributeName:STheme.D4}];
        self.tfMain.attributedPlaceholder = attr;
    }
}
- (void)setStrInput:(NSString *)strInput{
    _strInput = strInput;
    self.tfMain.text = self.strInput;
    [self textFieldDidChangeValue:self.tfMain];
}
- (void)setIsEditing:(BOOL)isEditing{
    _isEditing = isEditing;
    self.tfMain.userInteractionEnabled = isEditing;
}
- (void)setKeyboardType:(UIKeyboardType)keyboardType{
    _keyboardType = keyboardType;
    self.tfMain.keyboardType = keyboardType;
}
- (void)setIsPwd:(BOOL)isPwd{
    _isPwd = isPwd;
    self.tfMain.secureTextEntry = isPwd;
}
- (void)setTfTag:(NSInteger)tfTag{
    _tfTag = tfTag;
    self.tfMain.tag = self.tfTag;
}
- (void)setShowBtn:(BOOL)showBtn{
    self.btnCode.hidden = !showBtn;
    [self.btnCode mas_updateConstraints:^(MASConstraintMaker *make) {
        if (showBtn) {
            make.width.mas_equalTo([SFitUtil fit:120]);
        }else{
            make.width.mas_equalTo([SFitUtil fit:0]);
        }
    }];
}
- (void)setStrBtn:(NSString *)strBtn{
    [self.btnCode setTitle:strBtn forState:UIControlStateNormal];
}
- (void)setTfAlignment:(NSTextAlignment)tfAlignment{
    _tfAlignment = tfAlignment;
    self.tfMain.textAlignment = tfAlignment;
}

#pragma mark -- Event action
- (void)textFieldDidChangeValue:(UITextField *)sender{
    NSString *text = [sender.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    sender.text = text;
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFiledChanged:)]) {
        [self.delegate textFiledChanged:sender];
    }
}

- (void)codeButtonAction:(UIButton *)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(getMoboleCode:)]) {
        [self.delegate getMoboleCode:sender];
    }
}

#pragma mark -- setter and getter
- (UILabel *)lblTitle{
    if (_lblTitle == nil) {
        _lblTitle = [[UILabel alloc] init];
        _lblTitle.textColor = STheme.D1;
        _lblTitle.font = [SFitUtil fitBoldFont:16];
    }
    return _lblTitle;
}
- (UITextField *)tfMain{
    if (_tfMain == nil){
        _tfMain = [[UITextField alloc] init];
        _tfMain.borderStyle = UITextBorderStyleNone;
        _tfMain.textColor = STheme.D1;
        _tfMain.font = [SFitUtil fitBoldFont:16];
        _tfMain.backgroundColor = [UIColor clearColor];
        _tfMain.returnKeyType = UIReturnKeyDone;
        _tfMain.keyboardType = UIKeyboardTypeDefault;
        _tfMain.clearButtonMode = NO;
        _tfMain.textAlignment = NSTextAlignmentRight;
        [_tfMain addTarget:self
                action:@selector(textFieldDidChangeValue:)
        forControlEvents:UIControlEventEditingChanged];
    }
    return _tfMain;
}
- (UIButton *)btnCode{
    if (_btnCode == nil) {
        _btnCode = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnCode setTitleColor:STheme.D12 forState:UIControlStateNormal];
        [_btnCode setTitleColor:STheme.D4 forState:UIControlStateDisabled];
        _btnCode.titleLabel.font = [SFitUtil fitFont:16];
        _btnCode.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _btnCode.titleLabel.adjustsFontSizeToFitWidth = YES;
        _btnCode.titleLabel.minimumScaleFactor = 0.6;
        [_btnCode addTarget:self action:@selector(codeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnCode.hidden = YES;
    }
    return _btnCode;
}


@end
